package com.boss.thirteen.dao.impl;

import com.boss.common.util.DateUtils;
import com.boss.framework.dao.BaseDaoImpl;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.constant.SystemDictConstant;
import com.boss.salary.constant.TableName;
import com.boss.thirteen.dao.ISalaryThirteenMouldDao;
import com.boss.sys.utils.SysUtils;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Repository
public class SalaryThirteenMouldDaoImpl extends BaseDaoImpl implements ISalaryThirteenMouldDao {
    @Override
    public List<Map<String, Object>> querySalaryThirteenMouldPage(PaginationDTO paginationDTO,String salary_year,String province) {
        List<Object> paramList = new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        sql.append(" select id, biz_key, create_time, update_time, merit_pay_code, salary_codes, mof_div_code, is_deleted, salary_year year, is_end, mould_month,percent,is_workflow_start ")
                .append(" from ").append(TableName.THIRTEEN_MOULD)
                .append(" where is_deleted=? and mof_div_code=? and salary_year=? ");
        paramList.add(SystemDictConstant.NOT_DELETED);
        paramList.add(province);
        paramList.add(salary_year);
        sql.append(" order by merit_pay_code ");
        String querySql = this.boxUpSql(sql.toString());
        Object[] params = boxUpParam(paramList.toArray(), paginationDTO);
        return super.getjdbcTemplate().queryForList(querySql,params);
    }

    @Override
    public int querySalaryThirteenMouldCount( String salary_year, String province) {
        List<Object> paramList = new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        sql.append(" select count(*) ")
                .append(" from ").append(TableName.THIRTEEN_MOULD)
                .append(" where is_deleted=? and mof_div_code=? and salary_year=? ");
        paramList.add(SystemDictConstant.NOT_DELETED);
        paramList.add(province);
        paramList.add(salary_year);
        return this.getjdbcTemplate().queryForObject(sql.toString(), paramList.toArray(), Integer.class);
    }

    @Override
    public List<Map<String, Object>> selectSalaryThirteenMould(String province,String salary_year) {
        List<Object> paramList = new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        sql.append(" select id, biz_key, create_time, update_time, merit_pay_code, salary_codes, mof_div_code, is_deleted, salary_year year, is_end, mould_month,percent,is_workflow_start ")
                .append(" from ").append(TableName.THIRTEEN_MOULD)
                .append(" where is_deleted=? and mof_div_code=? and salary_year=? ");
        paramList.add(SystemDictConstant.NOT_DELETED);
        paramList.add(province);
        paramList.add(salary_year);
        sql.append(" order by merit_pay_code ");
        return super.getjdbcTemplate().queryForList(sql.toString(),paramList.toArray());
    }

    @Override
    public void batchInsertSalaryThirteenMould(List<Map<String, Object>> list) {
        StringBuilder sql = new StringBuilder();
        sql.append(" INSERT INTO  ").append(TableName.THIRTEEN_MOULD);
        sql.append("(id, biz_key, create_time, update_time, merit_pay_code, salary_codes, mof_div_code, is_deleted, salary_year, is_end,mould_month,percent,is_workflow_start) ");
        sql.append(" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
        List<Object[]> params=new ArrayList<>();
        for(Map<String, Object> map:list)
            params.add(new Object[] {
                    SysUtils.generateGuid(),map.get("mouldKey") ,String.valueOf(DateUtils.getCurrentTimeStamp()),String.valueOf(DateUtils.getCurrentTimeStamp()),
                    map.get("meritPayCode"),map.get("salaryCodes"),map.get("mof_div_code"),
                    SystemDictConstant.NOT_DELETED,map.get("year"),SystemDictConstant.IS_FALSE,map.get("month"),map.get("percent"),SystemDictConstant.IS_FALSE
            });
        this.getjdbcTemplate().batchUpdate(sql.toString(), new BatchPreparedStatementSetter() {
            public int getBatchSize() {
                return params.size();
                //这个方法设定更新记录数，通常List里面存放的都是我们要更新的，所以返回list.size();
            }
            public void setValues(PreparedStatement ps, int i)throws SQLException {
                Object[] sme = params.get(i);
                for(int a = 0; a < sme.length; a++){
                    ps.setObject(a+1,sme[a]);
                }

            }
        });
    }

    @Override
    public void batchUpdateSalaryThirteenMould(List<Map<String, Object>> list) {
        StringBuilder sql = new StringBuilder();
        sql.append(" update  ").append(TableName.THIRTEEN_MOULD);
        sql.append(" set update_time=?, salary_codes=?,mould_month=?,percent=? ");
       sql.append(" where biz_key=? and mof_div_code=? and merit_pay_code=? ");
        List<Object[]> params=new ArrayList<>();
        for(Map<String, Object> map:list)
            params.add(new Object[] {
                    String.valueOf(DateUtils.getCurrentTimeStamp()),map.get("salaryCodes"),map.get("month"),map.get("percent"),
                    map.get("mouldKey"),map.get("mof_div_code"),map.get("meritPayCode")
            });
        this.getjdbcTemplate().batchUpdate(sql.toString(), new BatchPreparedStatementSetter() {
            public int getBatchSize() {
                return params.size();
                //这个方法设定更新记录数，通常List里面存放的都是我们要更新的，所以返回list.size();
            }
            public void setValues(PreparedStatement ps, int i)throws SQLException {
                Object[] sme = params.get(i);
                for(int a = 0; a < sme.length; a++){
                    ps.setObject(a+1,sme[a]);
                }

            }
        });
    }

    @Override
    public void deleteSalaryThirteenMould(String mouldKey) {
        StringBuilder sql = new StringBuilder();
        sql.append(" update  ").append(TableName.THIRTEEN_MOULD);
        sql.append(" set update_time=?, is_deleted=?");
        sql.append(" where biz_key=? ");
        Object[] objects=new Object[]{
                String.valueOf(DateUtils.getCurrentTimeStamp()) ,SystemDictConstant.IS_DELETED,mouldKey
        };
        this.getjdbcTemplate().update(sql.toString(),objects);
    }

    @Override
    public void updateSalaryThirteenMouldStatus(String mouldKey, String isEnd,String isWorkflowStart) {
        List<Object> param=new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        sql.append(" update  ").append(TableName.THIRTEEN_MOULD);
        sql.append(" set update_time=?, is_end=? ");
        param.add(String.valueOf(DateUtils.getCurrentTimeStamp()));
        param.add(isEnd);
        if(!SysUtils.isNullOrEmpty(isWorkflowStart)){
            sql.append(",is_workflow_start=? ");
            param.add(isWorkflowStart);
        }
        sql.append(" where biz_key=? ");
        param.add(mouldKey);
        this.getjdbcTemplate().update(sql.toString(),param.toArray());
    }

    @Override
    public List<String> queryMouldSalaryCodes( List<String> personTypes,String province,String salary_year) {
        List<Object> paramList = new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        sql.append(" select  salary_codes ")
                .append(" from ").append(TableName.THIRTEEN_MOULD)
                .append(" where is_deleted=? and mof_div_code=? and salary_year=? ");
        paramList.add(SystemDictConstant.NOT_DELETED);
        paramList.add(province);
        paramList.add(salary_year);
        if(!SysUtils.isNullOrEmpty(personTypes)) {
            sql.append(" and ").append(SysUtils.inSql("merit_pay_code", personTypes));
        }

        sql.append(" order by merit_pay_code ");
        return super.getjdbcTemplate().queryForList(sql.toString(),paramList.toArray(),String.class);
    }

    @Override
    public String getMaxMouldYear( String province) {
        List<Object> paramList = new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        sql.append(" select max(salary_year) ")
                .append(" from ").append(TableName.THIRTEEN_MOULD)
                .append(" where is_deleted=? and mof_div_code=?  ");
        paramList.add(SystemDictConstant.NOT_DELETED);
        paramList.add(province);
        return this.getjdbcTemplate().queryForObject(sql.toString(), paramList.toArray(), String.class);
    }
}
